package com.barbeque.mall.service.mallmanagement;

import com.barbeque.mall.bean.BasePageInfo;
import com.barbeque.mall.bean.keyword.MarketKeyword;
import com.barbeque.mall.bean.keyword.MarketKeywordExample;
import com.barbeque.mall.bean.param.CommonData;
import com.barbeque.mall.mapper.MarketKeywordMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author ycc
 * @date 2022/07/16 14:21:42
 */
@Service
public class KeywordServiceImpl implements KeywordService {

    @Autowired
    MarketKeywordMapper keywordMapper;

    @Override
    public CommonData<MarketKeyword> list(BasePageInfo pageInfo, MarketKeyword marketKeyword) {
        //开启分页
        PageHelper.startPage(pageInfo.getPage(),pageInfo.getLimit());

        String sort = pageInfo.getSort();
        String order = pageInfo.getOrder();

        MarketKeywordExample example = new MarketKeywordExample();
        //拼接orderClause语句
        if (StringUtils.isEmpty(sort) && StringUtils.isEmpty(order)){
            //order by add_time desc
            example.setOrderByClause(sort+" "+order);
        }

        //拼接select语句
        MarketKeywordExample.Criteria criteria = example.createCriteria();
        //显示未被删除的关键词信息
        criteria.andDeletedEqualTo(false);
        //搜索关键词
        String keyword = marketKeyword.getKeyword();
        String url = marketKeyword.getUrl();
        if (!StringUtils.isEmpty(keyword)) {
            keyword = "%"+keyword+"%";
            criteria.andKeywordLike(keyword);
        }
        if (!StringUtils.isEmpty(url)) {
            url = "%"+url+"%";
            criteria.andUrlLike(url);
        }

        List<MarketKeyword> issues = keywordMapper.selectByExample(example);

        PageInfo<MarketKeyword> info = new PageInfo<>(issues);

        return CommonData.data(info);
    }

    @Override
    public int create(MarketKeyword keyword) {
        keyword.setAddTime(new Date());
        return keywordMapper.insertSelective(keyword);
    }

    @Override
    public int update(MarketKeyword keyword) {
        //更改修改时间
        keyword.setUpdateTime(new Date());

        MarketKeywordExample keywordExample = new MarketKeywordExample();
        MarketKeywordExample.Criteria criteria = keywordExample.createCriteria();
        criteria.andIdEqualTo(keyword.getId());
        //关键词id不能更改
        keyword.setId(null);
        //添加时间不能更改
        keyword.setAddTime(null);

        return keywordMapper.updateByExampleSelective(keyword, keywordExample);
    }

    @Override
    public int delete(MarketKeyword keyword) {
        MarketKeywordExample keywordExample = new MarketKeywordExample();
        MarketKeywordExample.Criteria criteria = keywordExample.createCriteria();
        //拼接select语句
        criteria.andIdEqualTo(keyword.getId());
        //关键词id不能更改
        keyword.setId(null);
        //添加时间不能更改
        keyword.setAddTime(null);
        //修改删除状态码
        keyword.setDeleted(true);

        return keywordMapper.updateByExampleSelective(keyword, keywordExample);
    }
}
